<template>
  <div>
    <el-button type="primary" @click="dialog = true">新增</el-button>

    <el-table :data="data" style="width: 100%" max-height="100vh">
      <el-table-column fixed prop="date" label="日期" width="150"></el-table-column>
      <el-table-column prop="_id" label="ID" width="120"></el-table-column>
      <el-table-column prop="title" label="标题" width="120"></el-table-column>
      <el-table-column prop="image" label="图片" width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="updataRow(scope.$index, data)"
            type="text"
            size="small"
          >编辑</el-button>
          <el-button
            @click.native.prevent="deleteRow(scope.$index, data)"
            type="text"
            size="small"
          >移除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-drawer
      title="编辑内容"
      :before-close="handleClose"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
    >
      <div class="demo-drawer__content" style="padding:0 40px;">
        <el-form :model="form">
          <el-form-item label="标题" :label-width="formLabelWidth">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="图片" :label-width="formLabelWidth">
            <el-input v-model="form.image" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="目标位置" :label-width="formLabelWidth">
            <el-input v-model="form.path" autocomplete="off" placeholder="请输入网址"></el-input>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="cancelForm">取 消</el-button>
          <el-button
            type="primary"
            @click="$refs.drawer.closeDrawer()"
            :loading="loading"
          >{{ loading ? '提交中 ...' : '确 定' }}</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<style>
</style>
<script>
export default {
  methods: {
    async deleteRow(index, rows) {
      await this.$http.delete(`Swiper/${rows[index]._id}`);
      this.fetch();
      this.$message({
        message: "恭喜您，删除成功！",
        type: "success"
      });
    },
    async createRow(rows) {
      this.loading = true;
      let model = {};
      if (this.form._id) {
        model = await this.$http.put(`Swiper/${this.form._id}`, {
          title: rows.title,
          image: rows.image,
          path: rows.path
        });
      } else {
        model = await this.$http.post("Swiper", rows);
      }
      if (model) {
        this.loading = false;
        this.dialog = false;
        this.fetch();
        this.form = {};
        this.$message({
          message: "恭喜你，请求成功！",
          type: "success"
        });
      } else {
        this.$message({
          message: "请求失败",
          type: "error"
        });
      }
    },
    async updataRow(index, rows) {
      this.form = rows[index];
      this.dialog = true;
    },
    async handleClose() {
      if (this.loading) {
        return;
      }
      this.$confirm("确定要提交表单吗？")
        .then(() => {
          if (this.form.title && this.form.image && this.form.path) {
            this.createRow(this.form);
          } else {
            this.$message({
              message: "信息输入不完整",
              type: "error"
            });
          }
        })
        .catch(() => {});
    },
    cancelForm() {
      this.loading = false;
      this.dialog = false;
      clearTimeout(this.timer);
    },
    async fetch() {
      const model = await this.$http.get("Swiper");
      this.data = model.data;
    }
  },
  created() {
    window.console.log("555");
    this.fetch();
  },
  data() {
    return {
      dialog: false,
      form: {},
      formLabelWidth: "80px",
      timer: null,
      loading: false,
      data: []
    };
  }
};
</script>